//
//  FinishInfoViewController.m
//  DaJiang
//
//  Created by sally on 16/10/9.
//  Copyright © 2016年 jiniu. All rights reserved.
//

#import "CellManager.h"
#import "FinishInfoViewController.h"
#import "UserModel.h"

@interface FinishInfoViewController () <UITextFieldDelegate, UIActionSheetDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIView *headerView;
@property (weak, nonatomic) IBOutlet UIImageView *headImage;
@property (strong, nonatomic) NSArray *tableData;
@property (strong, nonatomic) UITextField *textField;

@end

@implementation FinishInfoViewController

//注册或者登录或进入app时，必须要完善信息，不然不让进入。因为环信聊天需要。
- (void)viewDidLoad {
  [super viewDidLoad];
  [self initControl];
  [self initData];
}

#pragma mark 初始化控件
- (void)initControl {
  self.title = @"完善信息";
  self.navigationItem.hidesBackButton = self.isHiddenBack;
  self.headerView.height = 117;
  UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickOnHead)];
  [self.headerView addGestureRecognizer:tap];

  [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
  [[CommonMethod sharedCommonMethod] setExtraCellLineHidden:self.tableView];
  UserModel *userModel = [[CacheManager shareInstance]getLastLoginAccount];
  if (userModel) {
    [self.headImage sd_setImageWithURL:[NSURL URLWithString:userModel.user_data.user_profile_pic] placeholderImage:[UIImage imageNamed:@"ic_default"]];
  }
  self.headImage.layer.cornerRadius = GHeadImageHeight / 2;
  self.headImage.layer.masksToBounds = YES;
}

#pragma mark 初始化数据
- (void)initData {
  self.tableData = @[ @"真实姓名" ];
}

#pragma mark - action
#pragma mark 点击保存
- (IBAction)actionOnSave:(id)sender {
  if ([self.headImage.image isEqual:[UIImage imageNamed:@"ic_default"]]) {
    [[CommonMethod sharedCommonMethod]showToastInCenter:self.view withMessage:@"请上传头像"];
    return;
  }

  if (self.textField.text.length > 0) {
    //上传头像
    [[CommonMethod sharedCommonMethod]createProgressHUDInView:self.view withMessage:@""];
    [PicModel uploadPicWithImage:self.headImage.image completion:^(PicModel *pic) {
      if (pic) {
        //完善用户资料
        NSString *picUrl = pic.thumbnail_pic.length > 0 ? pic.thumbnail_pic : pic.original_pic;
        [self updateUserInfo:picUrl];
      } else {
        NSError *error = [PicModel getLastError];
        [[CommonMethod sharedCommonMethod] showAlertMessage:error.domain];
      }
    }];
  }else{
    [[CommonMethod sharedCommonMethod] showToastInCenter:self.view withMessage:@"请填写昵称"];
  }
}

#pragma mark - TableView data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  return self.tableData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];

  //主标题
  [CellManager configCellNormalText:cell];
  cell.textLabel.text = self.tableData[indexPath.row];

  //编辑行
  self.textField = [CellManager configCellNormalTextField:cell];
  self.textField.delegate = self;
  self.textField.tag = indexPath.row;
  [cell.contentView addSubview:self.textField];

  return cell;
}

#pragma mark - UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
  NSString *selectStr = [actionSheet buttonTitleAtIndex:buttonIndex];
  UIImagePickerController *imagePicker = [OperationManager configImagePicker:selectStr];
  if (imagePicker) {
    imagePicker.allowsEditing = YES;// 图片裁剪尺寸设定
  }else{
    return;
  }
}

#pragma mark UIImagePickerControllerDelegate 选照片
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *, id> *)info {
  [picker dismissViewControllerAnimated:YES completion:nil];

  //选择照片
  UIImage *originalImage = [info objectForKey:UIImagePickerControllerEditedImage];
  originalImage = [[CommonMethod sharedCommonMethod] compressImage:originalImage]; //压缩图片

  //装载图片操作
  self.headImage.image = originalImage;

  [self.tableView reloadData];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
  [picker dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - 私有方法
#pragma mark 点击上传头像
- (void)clickOnHead {
  [OperationManager configCameraActionSheet];
}

#pragma mark 完善用户资料
- (void)updateUserInfo:(NSString *)picUrl {
  [UserModel updateUserInfoWithUserName:self.textField.text userProfilePic:picUrl locationName:@"" bigPic:@"" proDesc:@"" proTitle:@"" topicIndex:@"" topicIndex2:@"" completion:^(UserModel *user) {
    if (user) {
      [[CommonMethod sharedCommonMethod]showToastInCenter:self.view withMessage:@"成功完善用户资料"];
      //缓存用户信息
      [[CacheManager shareInstance] cacheLastLoginAccount:user];
      //退出
      [self.navigationController popViewControllerAnimated:YES];

    } else {
      NSError *error = [UserModel getLastError];
      [[CommonMethod sharedCommonMethod] showAlertMessage:error.domain];
    }
    [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
  }];
}

@end
